package com.zking.web.TL;

import java.io.IOException;
import java.net.URLEncoder;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.zking.entity.Order;
import com.zking.service.BookService;
import com.zking.utils.DateTimeUtils;
import com.zking.utils.NumberUtils;
import com.zking.utils.ServletUtils;

@WebServlet("/Book")
public class BookServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;
	//传递数据给前端的jsonObject
	private JSONObject jsonObject = new JSONObject();
	//创建订单service类
	private BookService bookService = new BookService();
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
			String method = request.getParameter("method");
			if ("book".equals(method)) {
				book(request, response);
			}
	}
	
	
	/**
	 * 预定就餐的方法
	 * @param request
	 * @param response
	 */
	private void book(HttpServletRequest request, HttpServletResponse response) {
		//获取日期
		String day = request.getParameter("day");
		//获取时间
		String Time = request.getParameter("time");
		//拼接时间
		String ordertime = day +"  "+ Time ;
		//获取用餐人数
		int person_number = NumberUtils.parseStr(request.getParameter("personNumb"));
//		System.out.println("person_number="+person_number);
		//获取当前时间
		String currentTime = DateTimeUtils.getTimeStr();
		//创建订单类
		Order order = new Order();
		order.setOrdertime(ordertime);
		order.setPerson_number(person_number);
		//设置创建订单时间
		order.setTime(currentTime);
		int i = 0;
			try {
				//创建订单初始数据
				i = bookService.createBook(order);
				jsonObject.clear();
				jsonObject.fluentPut("code", i>0?i:-1);
				ServletUtils.send(response, jsonObject);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
	}
	
}
